package org.cjprods.ars;

import javax.json.Json;
import javax.json.JsonValue;
import org.cjprods.ars.annotations.arg;
import org.cjprods.ars.annotations.path;
import org.cjprods.ars.annotations.post;
import org.cjprods.ars.annotations.result;
import org.cjprods.ars.annotations.sql;
import org.cjprods.ars.annotations.token;

public class services
{
    // SPECIAL
    public static JsonValue no_such_api()
    {
        throw new utils.failure("no such api");
    }

// <editor-fold desc="user">
    @path("/user/login")
    @post
    @sql(func="user_info",
         values={"username"},
         results={"seq", "credential", "role", "name", "phone"})
    public static JsonValue user_login(
            @arg("password") String password,
            @result("credential") String credential,
            @result("seq") String seq,
            @result("role") String role,
            @result("name") String name,
            @result("phone") String phone)
    {
        if (!utils.check_password(credential, password))
            throw new utils.failure("auth failed");

        String token = utils.make_token(seq, role);
        return Json.createObjectBuilder()
                .add("token", token)
                .add("name", name)
                .add("phone", phone)
                .build();
    }

    @path("/user/validate")
    @token
    @sql(func="validate_user",
         values={"#"},
         results={"username", "name", "phone"})
    public static void user_validate() {}

    @path("/user/add")
    @post
    @sql(func="add_user",
         values={"username", "*password", "name", "phone"})
    public static void user_add() {}

    @path("/user/list")
    @token(roles={"admin"})
    @sql(func="list_users",
         values={"after", "search"},
         results={"id", "username", "name", "phone"},
         multi=true)
    public static void user_list() {}

    @path("/user/password")
    @post
    @token(roles={"admin"})
    @sql(func="modify_password",
         values={"user", "*new"})
    public static void user_password() {}

    @path("/user/password/self")
    @post
    @token
    @sql(func="modify_password",
         values={"#", "*new"})
    public static void user_password_self() {}

    @path("/user/name/self")
    @token(roles={"user"})
    @sql(func="modify_name",
         values={"#", "new"})
    public static void user_name_self() {}

    @path("/user/phone/self")
    @token(roles={"user"})
    @sql(func="modify_phone",
         values={"#", "new"})
    public static void user_phone_self() {}
// </editor-fold>

// <editor-fold desc="news">
    @path("/news/list")
    @sql(func="list_newss",
         values={"after", "search"},
         results={"id", "body", "date"},
         multi=true)
    public static void news_list() {}

    @path("/news/add")
    @post
    @token(roles={"admin"})
    @sql(func="add_news",
         values={"body"})
    public static void news_add() {}

    @path("/news/body")
    @post
    @token(roles={"admin"})
    @sql(func="modify_news",
         values={"news", "new"})
    public static void news_body() {}

    @path("/news/remove")
    @token(roles={"admin"})
    @sql(func="remove_news",
         values={"news"})
    public static void news_remove() {}

// </editor-fold>

// <editor-fold desc="feedback">
    @path("/feedback/list")
    @token(roles={"admin"})
    @sql(func="list_feedbacks",
         values={"after", "search"},
         results={"id", "who", "body", "reply"},
         multi=true)
    public static void feedback_list() {}

    @path("/feedback/list/self")
    @token(roles={"user"})
    @sql(func="list_my_feedbacks",
         values={"#", "after", "search"},
         results={"id", "body", "reply"},
         multi=true)
    public static void feedback_list_self() {}

    @path("/feedback/add")
    @post
    @token(roles={"user"})
    @sql(func="add_feedback",
         values={"#", "body"})
    public static void feedback_add() {}

    @path("/feedback/remove/self")
    @token(roles={"user"})
    @sql(func="remove_my_feedback",
         values={"#", "feedback"})
    public static void feedback_remove_self() {}

    @path("/feedback/reply")
    @post
    @token(roles={"admin"})
    @sql(func="reply_feedback",
         values={"feedback", "reply"})
    public static void feedback_reply() {}
// </editor-fold>

// <editor-fold desc="autos">
    @path("/auto/list/available")
    @sql(func="list_available_autos",
         values={"after", "search"},
         results={"id", "name", "description", "price"},
         multi=true)
    public static void auto_list_available() {}

    @path("/auto/list")
    @token(roles={"admin"})
    @sql(func="list_autos",
         values={"after", "search"},
         results={"id", "name", "description", "price", "available"},
         multi=true)
    public static void auto_list() {}

    @path("/auto/add")
    @post
    @token(roles={"admin"})
    @sql(func="add_auto",
         values={"name", "description", "price"})
    public static void auto_add() {}

    @path("/auto/remove")
    @token(roles={"admin"})
    @sql(func="remove_auto",
         values={"auto"})
    public static void auto_remove() {}
// </editor-fold>

// <editor-fold desc="orders">
    @path("/order/list")
    @token(roles={"admin"})
    @sql(func="list_orders",
         values={"after", "search"},
         results={"id", "orderer", "phone", "auto_name", "auto_description", "price", "state"},
         multi=true)
    public static void order_list() {}

    @path("/order/list/self")
    @token(roles={"user"})
    @sql(func="list_my_orders",
         values={"#", "after", "search"},
         results={"id", "auto_name", "auto_description", "price", "state"},
         multi=true)
    public static void order_list_self() {}

    @path("/order/add")
    @post
    @token(roles={"user"})
    @sql(func="add_order",
         values={"#", "auto"})
    public static void order_add() {}

    @path("/order/cancel/self")
    @token(roles={"user"})
    @sql(func="cancel_my_order",
         values={"#", "order"})
    public static void order_cancel_self() {}

    @path("/order/cancel")
    @token(roles={"admin"})
    @sql(func="cancel_order",
         values={"order"})
    public static void order_cancel() {}

    @path("/order/deliver")
    @token(roles={"admin"})
    @sql(func="deliver_order",
         values={"order"})
    public static void order_deliver() {}

    @path("/order/return")
    @token(roles={"admin"})
    @sql(func="return_order",
         values={"order"})
    public static void order_return() {}
// </editor-fold>

}

